<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <meta name="referrer" content="no-referrer" />
  <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
  <meta http-equiv="Pragma" content="no-cache" />
  <meta http-equiv="Expires" content="0" />
  <title>提现服务授权</title>
  <style>
    #success, #closetips {
      display: none;
      text-align: center;
      margin-top: 250px;
      color: #1e80ff;
      font-size: 20px;
    }
    #closetips {
      margin-top: 10px;
      color: #999;
      font-size: 16px;
    }
  </style>
</head>

<body>
  <noscript>
    <strong>提现服务授权</strong>
  </noscript>
  <div id="success">授权成功!</div>
  <div id="closetips">3秒钟后自动关闭页面</div>
  <!-- <script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script> -->
  <script src="./static/js/base.js"></script>
  <script src="./static/js/alipayjsapi.min.js"></script>
  <script>
    // window.vConsole = new window.VConsole({
    //   defaultPlugins: ['system', 'network', 'element', 'storage'],
    //   // 可以在此设定要默认加载的面板
    //   maxLogNumber: 1000,
    //   disableLogScrolling: true,
    //   onReady: () => {
    //     console.log('vConsole: onReady');
    //   },
    //   onClearLog: () => {
    //     console.log('vConsole: onClearLog');
    //   }
    // });

    let browser = navigator.userAgent.toLowerCase();
    const urlParams = new URLSearchParams(window.location.search);
    const pappId = urlParams.get('pappId');
    if (browser.match(/Alipay/i) == "alipay") {
      ap.getAuthCode({
        appId: pappId,
        scopes: ['auth_user'],
      }, function (res) {
        if (res.authCode) {
          console.log('res.authCode:', res.authCode);
          bindAuth(res.authCode)
        }
        console.log('res:', res);
      });
    } else {
      document.querySelector('#success').style.display = 'block'
      document.querySelector('#success').innerHTML = '请在支付宝APP打开页面'
    }

    // 关闭页面倒计时
    let timer = 0
    let timerId = null
    function setTimerId() {
      document.querySelector('#closetips').style.display = 'block'
      timer = 3
      timerId = setInterval(() => {
        timer--
        document.querySelector('#closetips').innerHTML = timer+'秒钟后自动关闭页面'
        console.log(timer)
        if (timer <= 0) {
          clearInterval(timerId)
          timerId = null
          timer = 0
          //这个可以关闭安卓系统的手机-支付宝
          document.addEventListener("AlipayJSBridgeReady", function () {
              AlipayJSBridge.call("closeWindow");
            },
            false
          );
          //这个可以关闭ios系统的手机-支付宝
          AlipayJSBridge.call('closeWebview');
        }
      }, 1000)
    }

    // 用户绑定平台
    function bindAuth(authCode) {
      console.log('authCode:', authCode);
      ajax({
        url: 'https://vip-xzdesktop-user.cqttech.com/api/v1/withdraw/bind',
        type: 'post',
        data: {
          "type": "alipay",
          "auth_code": authCode,
          "platform_appid": pappId,
          "bind_code": urlParams.get('codeid'),
        },
        headers: {
          'UnionId': urlParams.get('unionid'),
          'AppId': urlParams.get('appid'),
        },
        success(res) {
          if (res.code === 0 || res.code === 200) {
            document.querySelector('#success').style.display = 'block'
            setTimerId()
          } else {
            document.querySelector('#success').style.display = 'block'
            document.querySelector('#success').innerHTML = '授权失败，请重新扫码授权!'
            setTimerId()
          }
        }
      })
    }
  </script>
</body>

</html>
